<template>
  <!-- 系统得分明细目前只属于中交指标库 -->
  <div class="container">
    <div class="index-title">{{ routeQuery.name }}得分明细</div>
    <el-row class="" style="margin-bottom: 20px">
      <el-col :span="16" align="left">
        <el-input
          v-model="searchFrom.name"
          placeholder="名称"
          style="width: 150px; margin-right: 15px"
          @change="search"
          clearable
        />
        <el-select
          v-model="searchFrom.type"
          style="width: 150px; margin-right: 15px"
          @change="search"
          size="small"
          clearable
        >
          <el-option
            v-for="item in state.scoreDetailTypes"
            :key="item.itemValue"
            :label="item.itemText"
            :value="item.itemValue"
          >
          </el-option>
        </el-select>
        <el-button class="ordinary" @click="search()">搜索</el-button>
      </el-col>
      <el-col :span="8" align="right">
        <el-button class="ordinary" @click="getExportBtn()">批量导出</el-button>
      </el-col>
    </el-row>
    <div>
      <el-table
        :data="state.table_list"
        style="width: 100%"
        border
        @selection-change="selectionChange"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="selection"
          align="center"
          width="55"
          :selectable="checkSelect"
          :reserve-selection="false"
        />
        <el-table-column
          prop="ksType_text"
          align="center"
          label="科技实力类型"
          width="80"
        />
        <el-table-column prop="ksName" align="center" label="名称" width="80" />
        <el-table-column prop="orgName" align="center" label="单位" width="80" />
        <el-table-column prop="type_text" align="center" label="类型" width="80">
        </el-table-column>
        <el-table-column prop="examineFactor" align="center" label="系数" width="80">
        </el-table-column>
        <el-table-column prop="systemScore" align="center" label="系统得分" width="80">
        </el-table-column>
        <el-table-column prop="score" align="center" label="人工修订" width="140">
          <template #default="scope">
            <el-input-number
              v-model="state.manualScore"
              :controls="false"
              :max="99"
              :min="0"
              style="width: 120px"
              v-if="state.isRevise == scope.$index"
            />
            <span v-else>{{ scope.row.manualScore }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="scoreCompose" align="center" label="修订理由" width="140">
          <template #default="scope">
            <el-input
              v-if="state.isRevise == scope.$index"
              :maxlength="100"
              v-model="state.manualReason"
            ></el-input>
            <span v-else>{{ scope.row.manualReason }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="finalScore" align="center" label="最终得分" width="80" />
        <el-table-column
          prop="enterPeopleUsername"
          align="center"
          label="录入人"
          width="80"
        />
        <el-table-column prop="enterDatetime" align="center" label="录入时间" />
        <el-table-column
          prop="manualPeopleUsername"
          align="center"
          label="修定人"
          width="80"
        />
        <el-table-column prop="manualDatetime" align="center" label="修定时间" />
        <el-table-column
          prop="address"
          fixed="right"
          align="center"
          label="操作"
          width="150"
        >
          <template #default="scope">
            <span v-if="state.annualDetail && scope.row.listType != 'sum'">
              <el-button
                class="ordinary"
                @click="handleRevise(scope.row, scope.$index)"
                v-if="state.isRevise != scope.$index"
                :disabled="state.annualDetail.status == '3'"
                >修订</el-button
              >
              <el-button
                class="ordinary"
                @click="confirmRevise(scope.row)"
                v-if="state.isRevise == scope.$index"
                :disabled="state.annualDetail.status == '3'"
                >保存</el-button
              >
              <el-button type="primary" @click="handleDetail(scope.row)">查看</el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script lang="ts" setup>
import pubFc from "@/views/examineManage/utils";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import * as api from "../../../../api/examineManage";
interface Cdata {
  [propName: string]: any;
}
const searchFrom = reactive({
  //查询
  name: "",
  type: "",
});
const route = useRoute();
const routeQuery = route.query;
// const assignParams = reactive({})
let state = reactive<Cdata>({
  table_list: [],
  isRevise: -1, //是否处于修订模式
  handleRowData: null, //正在处理的行数据
  table_loading: false,
  scoreDetailTypes: [
    {
      itemText: "增量",
      itemValue: "1",
    },
    {
      itemText: "存量",
      itemValue: "2",
    },
  ],
  indexDeteil: null,
  annualDetail: null,
  manualScore: undefined,
  manualReason: "",
});
let selectedRows = ref<any>([]);
const search = () => {
  //搜索
  gettargetlist();
};

const getExportBtn = () => {
  //批量导出
  ElMessageBox.confirm("确定要批量导出吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    api
      .exportZjScoreRequest({
        ...searchFrom,
        id: routeQuery.id,
      })
      .then((res) => {
        const { data, headers } = res;
        pubFc.downloadFunc(data, headers);
      });
  });
};
const checkSelect = (row, index) => {
  let isChecked = true;
  if (index === 0) {
    // 判断里面是否存在某个参数
    isChecked = false;
  } else {
    isChecked = true;
  }
  return isChecked;
};
const selectionChange = (arr) => {
  //选中
  selectedRows.value = arr;
};

const handleRevise = (item, index) => {
  //编辑
  state.manualScore = item.manualScore || undefined;
  state.manualReason = item.manualReason || "";
  state.handleRowData = item;
  state.isRevise = index;
};

const router = useRouter();
const handleDetail = (item) => {
  console.log("查看", item.ksSonType);
  switch (item.ksSonType) {
    case "1":
      console.log("高端研发平台");
      router.push("/science/construction/high-end-platform");
      break;
    case "2":
      console.log("协同创新平台");
      router.push("/science/construction/innovate-platform");
      break;
    case "3":
      console.log("国际科技合作基地");
      router.push("/science/construction/technology-cooperation");
      break;
    case "4":
      console.log("国际人才交流");
      router.push("/science/InnovativeTalents/talentTeam");
      break;
    case "5":
      console.log("科技领军人才");
      router.push("/science/InnovativeTalents/technology-leadership");
      break;
    case "6":
      console.log("创新人才团队");
      router.push("/science/InnovativeTalents/talentTeam");
      break;
    case "7_1":
      console.log("授权专利");
      router.push("/science/payoffs/patent");
      break;
    case "7_2":
      console.log("授权软著");
      router.push("/science/payoffs/softwarecopyright");
      break;
    case "8_1":
      console.log("学术著作");
      router.push("/science/payoffs/composition");
      break;
    case "8_2":
      console.log("学术论文");
      router.push("/science/payoffs/thesis");
      break;
    case "9":
      console.log("技术标准项目");
      router.push("/science/contribute/technical-norms");
      break;
    case "10":
      console.log("外部奖励");
      router.push("/science/award/achievement");
      break;
    case "11":
      console.log("成果评价");
      router.push("/science/payoffs/assessmentresults");
      break;
  }
  //查看
};
const confirmRevise = (row:any) => {
  if(Number(state.manualScore)>Number(row.systemScore)){
    ElMessage({
      message: "人工修订分数不得大于系统得分!",
      type: "warning",
    });
    return  false
  }
  //保存修订数据
  api
    .reviseZjScoreRequest({
      id: row.id,
      manualScore: state.manualScore,
      manualReason: state.manualReason,
    })
    .then((res) => {
      if (res.data.code == 200) {
        ElMessage.success("已修订");
        state.isRevise = -1;
        gettargetlist();
      }
    });
};
const getAnnualDetail = () => {
  
  //先查询详情再根据详情中的状态来判断页面
  return new Promise((resolve, reject) => {
    api
      .annualDetailRequest({
        id: routeQuery.aid,
      })
      .then((res) => {
        state.annualDetail = res.data.data;
        resolve(1);
      });
  });
};
const geZjIndexDetailRequest = () => {
  state.table_loading = true;
  return new Promise((resolve) => {
    api
      .geZjIndexDetailRequest({
        id: routeQuery.id,
      })
      .then((res) => {
        if (res.data.code == 200) {
          state.indexDeteil = res.data.data;
          resolve(1);
        }
      });
  });
};
const gettargetlist = () => {
  //获取table列表
  state.table_loading = true;
  api
    .scoreDetailRequest({
      ...searchFrom,
      id: routeQuery.id, //1-中交标准指标、2-中咨标准指标
    })
    .then((res) => {
      state.table_loading = false;
      let objData = {
        ksName: "合计",
        systemScore: 0,
        finalScore: 0,
        listType: "sum",
      };
      for (let i = 0; i < res.data.data.length; i++) {
        objData.systemScore += res.data.data[i].systemScore || 0;
        objData.finalScore += res.data.data[i].finalScore || 0;
      }
      res.data.data.unshift(objData);
      state.table_list = res.data.data;

    });
};
getAnnualDetail();
geZjIndexDetailRequest().then(() => {
  gettargetlist();
});
</script>
<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
  overflow-y: auto;
  .index-title {
    text-align: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #e7e5e5;
    margin-bottom: 10px;
    font-size: 26px;
  }
}
</style>
